/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package docview;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

/**
 *
 * @author Dema
 */
public class DocTextArea extends JPanel implements DocTextListener {
    protected JTextArea textArea;
    protected JScrollPane scroller;
    protected DocTextListener listener = null;

    public DocTextArea(int height,int width)
    {
        super();
        textArea = new JTextArea();
        textArea.setEditable(true);
        textArea.setLineWrap(true);
        textArea.addKeyListener(new KeyHandler() );
        //textArea.getDocument().addDocumentListener(new DocTextDocumentListener() );
        scroller = new JScrollPane(textArea);
        //scroller.setPreferredSize(new Dimension(height,width));
        setLayout(new BorderLayout());
        add(scroller,BorderLayout.CENTER);
    }

    DocTextArea() {
               super();
        textArea = new JTextArea();
        textArea.setEditable(true);
        textArea.setLineWrap(true);
        //textArea.addKeyListener(new KeyHandler() );
        textArea.setFont(new Font(Font.SANS_SERIF,Font.ITALIC,12));
        textArea.getDocument().addDocumentListener(new DocTextDocumentListener() );
        scroller = new JScrollPane(textArea);
        //scroller.setPreferredSize(new Dimension(height,width));
        //***********************************commented by Gleb
        //setLayout(new BorderLayout());
        //add(scroller,BorderLayout.CENTER);
        setLayout(new GridLayout(1,1));
        add(scroller);
    }

    private class KeyHandler implements KeyListener
    {

        public void keyTyped(KeyEvent ke) {

            if (listener!=null)
            {
                try {

                    listener.notifyTextChange(textArea.getDocument().getText(0, textArea.getDocument().getLength()));
                } catch (BadLocationException ex) {
                    Logger.getLogger(DocTextArea.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        public void keyPressed(KeyEvent ke) {
           

        }

        public void keyReleased(KeyEvent ke) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

    };

    private class DocTextDocumentListener implements DocumentListener
    {

        public void insertUpdate(DocumentEvent de) {

            if (listener!=null)
            {
                try {

                    listener.notifyTextChange(de.getDocument().getText(0, de.getDocument().getLength()));
                } catch (BadLocationException ex) {
                    Logger.getLogger(DocTextArea.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        }

        public void removeUpdate(DocumentEvent de) {
            if (listener!=null)
            {
                try {

                    listener.notifyTextChange(de.getDocument().getText(0, de.getDocument().getLength()));
                } catch (BadLocationException ex) {
                    Logger.getLogger(DocTextArea.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        }

        public void changedUpdate(DocumentEvent de) {

        }
        
    };

    public void setListener(DocTextListener _listener) {
        listener = _listener;
    }

    public void notifyTextChange(String text) {
        textArea.setText(text);

    }

}
